package com.lbj.mall.product.controller;

import com.lbj.mall.common.utils.PageUtils;
import com.lbj.mall.common.utils.R;
import com.lbj.mall.product.entity.BrandEntity;
import com.lbj.mall.product.entity.CategoryBrandRelationEntity;
import com.lbj.mall.product.entity.CategoryEntity;
import com.lbj.mall.product.entity.vo.BrandVo;
import com.lbj.mall.product.service.BrandService;
import com.lbj.mall.product.service.CategoryBrandRelationService;
import com.lbj.mall.product.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 品牌分类关联
 *
 * @author libingjun
 * @email 617416791@qq.com
 * @date 2022-03-27 10:44:54
 */
@Slf4j
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取分类关联的品牌
     */
    @GetMapping("brands/list")
    public R getBrandListFromCategoryId(@RequestParam("catId") String categoryId) {
        List<CategoryBrandRelationEntity> brandList = categoryBrandRelationService.getBrandListFromCategoryId(categoryId);
        List<BrandVo> brandVoList = brandList.stream().map(item -> {
            BrandVo vo = new BrandVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        return R.ok().put("data", brandVoList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 品牌分类关联列表
     * brandId=9
     */
    @RequestMapping("/catelog/list")
    // @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam("brandId") Long brandId) {
        List<CategoryBrandRelationEntity> result = categoryBrandRelationService.queryById(brandId);
        return R.ok().put("data", result);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存,在这里我们还需要保存【品牌名】和【分类名】两个冗余字段到数据库中，方便我们查找
     * brandId: 9, catelogId: 225
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        // 保存品牌名冗余字段
        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        // 保存分类名冗余字段
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        log.info("保存品牌和分类关联的数据：" + categoryBrandRelation);
        return categoryBrandRelationService.save(categoryBrandRelation) ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
